using System;
using System.Data;
using System.Data.OleDb;

namespace Dbam.DbFramework
{
	/// <summary>
	/// Factory that supports working with OleDb databases.
	/// </summary>
	public class OleDbFactory
		: DbFactoryBase
	{
		/// <summary>
		/// Initialize the OleDbFactory with a connection to the database.
		/// </summary>
		/// <param name="connectionString">The connection to the database.</param>
		public OleDbFactory(string connectionString)
			: base(connectionString)
		{
		}

		/// <summary>
		/// Create a compatible connection to the OleDb database.
		/// </summary>
		/// <param name="connectionString">Connection string to the database.</param>
		/// <returns>The compatible OleDb connection object.</returns>
		protected override IDbConnection CreateConnection(string connectionString)
		{
			OleDbConnection connection = new OleDbConnection(connectionString);
			return connection;
		}

		/// <summary>
		/// Creates an OleDb-compatible parameter object.
		/// </summary>
		/// <param name="name">Name of the parameter.</param>
		/// <param name="type">Data type for the parameter.</param>
		/// <param name="direction">Direction of the parameter.</param>
		/// <param name="value">Value to assign the parameter.</param>
		/// <returns>A compatible OleDb parameter object.</returns>
		public override IDataParameter CreateParam(string name, DbType type, ParameterDirection direction, object value)
		{
			OleDbParameter param = new OleDbParameter();
			param.ParameterName = name;
			param.DbType = type;
			param.Direction = direction;
			param.Value = value;
			return param;
		}

		/// <summary>
		/// Creates a compatible OleDb data adapter object.
		/// </summary>
		/// <param name="selectCommand">The OleDb select command.</param>
		/// <param name="insertCommand">The OleDb insert command.</param>
		/// <param name="updateCommand">The OleDb update command.</param>
		/// <param name="deleteCommand">The OleDb delete command.</param>
		/// <returns>A compatible OleDb data adapter object.</returns>
		public override IDataAdapter CreateAdapter(IDbCommand selectCommand, IDbCommand insertCommand, IDbCommand updateCommand, IDbCommand deleteCommand)
		{
			if(selectCommand != null && !(selectCommand is OleDbCommand))
				throw new ArgumentException(ErrorStrings.IncorrectCommandType);
			if(insertCommand != null && !(insertCommand is OleDbCommand))
				throw new ArgumentException(ErrorStrings.IncorrectCommandType);
			if(updateCommand != null && !(updateCommand is OleDbCommand))
				throw new ArgumentException(ErrorStrings.IncorrectCommandType);
			if(deleteCommand != null && !(deleteCommand is OleDbCommand))
				throw new ArgumentException(ErrorStrings.IncorrectCommandType);

			OleDbDataAdapter adapter = new OleDbDataAdapter();
			adapter.SelectCommand = selectCommand as OleDbCommand;
			adapter.InsertCommand = insertCommand as OleDbCommand;
			adapter.UpdateCommand = updateCommand as OleDbCommand;
			adapter.DeleteCommand = deleteCommand as OleDbCommand;
			return adapter;
		}

		/// <summary>
		/// Creates a compatible OleDb command object.
		/// </summary>
		/// <param name="connection">The OleDb connection to use.</param>
		/// <param name="commandText">The command text.</param>
		/// <returns>A compatible OleDb command object.</returns>
		protected override IDbCommand CreateCommandCore(IDbConnection connection, string commandText)
		{
			OleDbCommand command = new OleDbCommand();
			command.Connection = connection as OleDbConnection;
			command.CommandText = commandText;
			return command;
		}
	}
}